﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using Htss.Platform.Web;
using Htss.Platform.Core;

namespace DreamBox.Web.Box.posts
{
    public  class addBox:ControllerBox
    {
       
        public ActionResult Load()
        {
            
            if (Request.RequestType == "POST")
            {
                if (Verification())
                {
                    Model.Posts m = new Model.Posts();
                    m.CID = int.Parse(Request.Form["PostsClass"]);
                    m.Title =Htss.Platform.Core.Text.Replace(Request.Form["Title"]);
                    m.Contents = Htss.Platform.Core.Text.ReplaceHtml(Request.Form["Contents"]);
                    m.Thumbnail = BLL.Posts.Thumbnail.Image(m.Contents);
                    m.UserID = int.Parse(Session["UserID"].ToString());

                    DAL.Posts.Posts posts = new DAL.Posts.Posts();
                    int PostsID = posts.Add(m);

                    if (PostsID > 0)
                    {
                        BLL.Message.Jump(this, "文字发布成功!", "/posts/read/" + PostsID, "1");
                    }
                }
            }
            //加载分类
            DAL.Posts.PostsClass pc = new DAL.Posts.PostsClass();
            ViewData["PostsClass"] = pc.GetClass(int.Parse(Session["GID"].ToString()));

            return View("posts_add");
        }
        bool Verification()
        {
            Verify vf = new Verify();
            vf.VerificationMessage = VerificationMessage;
            VerifyField vPostsClass = vf.NewVerifyField(Request.Form["PostsClass"],"PostsClass");
            vPostsClass.AddCustom("请选择分类", Request.Form["PostsClass"] == "0" ? false : true);

            VerifyField vTitle = vf.NewVerifyField(Request.Form["Title"], "Title");
            vTitle.AddNull("请输入标题");
            vTitle.AddLengthRange("请输入80个字符内的标题", 1, 80);

            VerifyField vContents = vf.NewVerifyField(Request.Form["Contents"],"Contents");
            vContents.AddNull("请输入内容");
            vf.Begin();
            return vf.IsVerify;
        }
    }
}
